// pages/businessInfoNext/businiessInfoNext.js
import imgUrl from "../../images/util.js";
import { getEnterprisesType, settleIn } from "../../ajax/module/userInfo.js";
let app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isNeedIcon: imgUrl.isNeedIcon,
    positionIcon: imgUrl.positionIcon,
    type: "请选择企业类型",
    typeArr: ["安防行业", "安电行业", "家政行业", "家具行业", "洗护行业"],
    temList: [],
    enterpriseTypeId: "",
    area: "请选择服务区域",
    textareaTxt: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let str = app.globalData.selectText || "请选择服务区域";
    this.setData({
      area: str,
    });

    getEnterprisesType().then((res) => {
      let list = res.data;
      let typeTextArr = [];
      list.forEach((item, index) => {
        typeTextArr.push(item.enterpriseTypeName);
      });
      this.setData({
        typeArr: typeTextArr,
        temList: list,
      });
      // console.log(list)
    });
  },
  jumpMap() {
    wx.navigateTo({
      url: "../mapPosition/mapPosition",
    });
  },
  jumpArea() {
    wx.navigateTo({
      url: "../service/serviceArea",
    });
  },
  PickerChange1(e) {
    console.log(e);
    let enterpriseTypeId = this.data.temList.filter((item) => {
      return item.enterpriseTypeName == this.data.typeArr[e.detail.value];
    })[0].enterpriseTypeId;
    console.log(enterpriseTypeId);
    this.setData({
      type: this.data.typeArr[e.detail.value],
      enterpriseTypeId: enterpriseTypeId,
    });
  },
  textareaAinput(e) {
    this.setData({
      textareaTxt: e.detail.value,
    });
  },
  jumpNext() {
    function vilidate(enterpriseType, area) {
      let flg = true;
      if (area == "请选择服务区域") {
        wx.showToast({
          title: "请选择服务区域",
          icon: "none",
          duration: 2000,
        });
        flg = false;
      }
      if (enterpriseType == "0") {
        wx.showToast({
          title: "请选择企业类型",
          icon: "none",
          duration: 2000,
        });
        flg = false;
      }
      return flg;
    }
    let enterpriseType = this.data.enterpriseTypeId;
    let area = this.data.area;
    if (vilidate(enterpriseType, area)) {
      app.globalData.information = {
        enterpriseType: enterpriseType,
      };
      let loginName, loginPwd;
      wx.getStorage({
        key: "abcKey",
        success: (res) => {
          // console.log(res)
          loginPwd = res.data;
        },
      });

      wx.getStorage({
        key: "loginName",
        success: (res) => {
          loginName = res.data;
        },
      });
      setTimeout(() => {
        let location = {
          latitude: app.globalData.selectedPosition.latitude,
          longitude: app.globalData.selectedPosition.longitude,
        };
        let serviceArea={
          serviceArea:app.globalData.selectText
        }
         

        let account = { loginName, loginPwd };
        let newData = {
          ...app.globalData.associatedLicense,
          ...app.globalData.describe,
          ...app.globalData.information,
          ...account,
          ...location,
          ...serviceArea
        };
        console.log(newData);
        settleIn(newData).then((res) => {
          console.log("hhhkkk", res.data);
          // wx.showToast({
          //   title: "入驻成功",
          //   duration: 2000,
          // });

          // let token = res.data.token;
          // let enterName = res.data.enterName;
          // let address = res.data.address;
          // app.globalData.loginInfoStore = { enterName, address };
          // app.globalData.account = {
          //   loginName: this.data.phone + "",
          //   loginPwd: this.data.pwd + "",
          // };
          // app.globalData.associatedLicense = {
          //   licenseCode: res.data.licenseCode,
          //   licenseImgUrl: res.data.licenseImgUrl,
          // };

          // setTimeout(() => {
          //   wx.setStorage({
          //     key: "token",
          //     data: token,
          //   });
          //   wx.setStorage({
          //     data: true,
          //     key: "isLogin",
          //   });
          //   wx.setStorage({
          //     data: status,
          //     key: "status",
          //   });

          //   wx.setStorage({
          //     data: { enterName, address },
          //     key: "loginInfoStore",
          //   });
          //   let {status}=res.data
          //   if (status == 1) {
          //     wx.setStorage({
          //       data: {
          //         licenseCode: res.data.licenseCode,
          //         licenseImgUrl: res.data.licenseImgUrl,
          //         enterName: res.data.enterName,
          //       },
          //       key: "associatedLicense",
          //     });

          //     wx.reLaunch({
          //       url: "../store-manager/store-manager",
          //     });
          //   }
          // }, 500);

               wx.reLaunch({
                url: "../settleSuccess/settleSuccess",
              });
        }).catch((e)=>{
           console.log(e)
          // wx.reLaunch({
          //   url: "../index/index",
          // });
          wx.showModal({
            content: e+"",
            confirmText: "确定",
            cancelText: "取消",
            success: res=> {

            if (res.confirm) {
                 wx.reLaunch({
            url: "../index/index",
          });
            }

			
          
            }
          })
        })
      }, 800);
    }

    // if(this.data.enterpriseTypeId)
  },
  bindInputMobile(e) {
    this.setData({
      mobile: e.detail.value,
    });
  },
  bindInputBusinessName(e) {
    this.setData({
      businessName: e.detail.value,
    });
  },

  bindInputName(e) {
    this.setData({
      name: e.detail.value,
    });
  },
  bindInputPhone(e) {
    this.setData({
      phone: e.detail.value,
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
